package javafx.table;

import java.util.List;

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.ProgressBarTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.table.TableViewSample.Person;
import javafx.util.Callback;

public class TestTable extends Application{
	 
	private ObservableList<TableDataBean> data = FXCollections.observableArrayList();
	private TableView mDownloadTable = new TableView();
	
	public void initData(){
		
		TableColumn<TableDataBean, String> name = new TableColumn<>("fileName");
		TableColumn<TableDataBean, Double> progress = new TableColumn<>("progress");
		TableColumn<TableDataBean, Boolean> sure = new TableColumn<>("sure");

		
		name.setCellValueFactory(new PropertyValueFactory("fileName"));
		name.setCellFactory(CenterTabelCell.forTableColumn());
		//可编辑
//		name.setCellFactory(TextFieldTableCell.<TableDataBean>forTableColumn());
//		name.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableDataBean,String>>() {
//			public void handle(CellEditEvent<TableDataBean, String> event) {
//				
//				TableDataBean rowValue = event.getRowValue();
//				rowValue.setFileName(event.getNewValue());
//				
//			}
//		});
		
		
		progress.setCellValueFactory(new PropertyValueFactory("progress"));
		progress.setCellFactory(ProgressBarTableCell.forTableColumn());
		
		sure.setCellValueFactory(new PropertyValueFactory("sure"));
//		sure.setCellFactory(CheckBoxTableCell.forTableColumn(sure));
		
		sure.setCellFactory(MyCheckBoxCell.forTableColumn());
		
		
		TableColumn<TableDataBean, List<Double>> chartcol = new TableColumn<>("chartDatas");
		chartcol.setCellValueFactory(new PropertyValueFactory("chartDatas"));
		
		chartcol.setCellFactory(new Callback<TableColumn<TableDataBean,List<Double>>, TableCell<TableDataBean,List<Double>>>() {

			public TableCell<TableDataBean, List<Double>> call(
					TableColumn<TableDataBean, List<Double>> chartDatas) {

				return new ChartTableCell();

			}
			
		});
		chartcol.setMinWidth(150);
		chartcol.setPrefWidth(200);
		chartcol.setMaxWidth(300);

		
		for(int i=0;i<100;i++){
			TableDataBean dd = new TableDataBean( "123.txt"+i,0.0+new Double(i)/100D);
			data.addAll(dd);
		}
		
		mDownloadTable.setItems(data);
		
		mDownloadTable.getColumns().addAll(name,sure,progress);
		mDownloadTable.setEditable(true);
		
		
		ObjectProperty rowFactoryProperty = mDownloadTable.rowFactoryProperty();
		
		mDownloadTable.fixedCellSizeProperty().set(45);
		
	}




	
	@Override
	public void start(Stage pStage) throws Exception {
		
		BorderPane root = new BorderPane();
		Scene scene = new Scene(root, 550,300);
		pStage.setScene(scene);
		
		addTable(root);

		pStage.centerOnScreen();
		pStage.show();
		
	}
	
	
	
	private void addTable(BorderPane root) {
		initData();
		root.setCenter(mDownloadTable);
	}





	public static void main(String[] args) {
		launch(args);
	}

}

class MyCheckBoxCell extends CheckBoxTableCell{
	
	@Override
	public void updateItem(Object item, boolean empty) {
		super.updateItem(item, empty);
		CheckBox cb = new CheckBox();
		
		 if (empty || item == null) {
	         setText(null);
	         setGraphic(null);
	     } else {
	         setText(item.toString());
	         cb.setSelected((Boolean)item);
	         setGraphic(cb);
	     }
		 
	}
	
	public static <S,T> Callback <TableColumn<S,T>, TableCell<S,T>> forTableColumn(){
		
		Callback<TableColumn<S,T>,TableCell<S,T> > call = new Callback<TableColumn<S,T>, TableCell<S,T>>() {

			@Override
			public TableCell<S, T> call(TableColumn<S, T> arg0) {
				return new MyCheckBoxCell();
			}
			
		};

		return call;
	}
}

class CenterTabelCell  extends TableCell {
	
	@Override
	protected void updateItem(Object value, boolean empty) {
		super.updateItem(value, empty);
		this.setAlignment(Pos.CENTER);
		if(value != null){
			this.setText(value.toString());
		}
	}
	
	public static <S,T> Callback <TableColumn<S,T>, TableCell<S,T>> forTableColumn(){
		
		Callback<TableColumn<S,T>,TableCell<S,T> > call = new Callback<TableColumn<S,T>, TableCell<S,T>>() {

			@Override
			public TableCell<S, T> call(TableColumn<S, T> arg0) {
				return new CenterTabelCell();
			}
			
		};

		return call;
	}
}
